﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using cpe.cpein.DATA;

namespace cpe.cpein.BUSINESS
{
    public static class BUGroupType
    {
        /// <summary>
        /// Get All the GroupType of the database.
        /// </summary>
        /// <param name="searchInDeleted"></param>
        /// <returns></returns>
        public static List<GroupType> GetAllGroupType(bool searchInDeleted)
        {
            List<GroupType> GroupTypes = new List<GroupType>();
            using (cpeinEntities db = new cpeinEntities())
            {
                var results = from GroupType in db.GroupType
                              where GroupType.grt_isdeleted == searchInDeleted
                              select GroupType;
                foreach (GroupType tmp in results)
                    GroupTypes.Add(tmp);
            }

            return GroupTypes;
        }

        /// <summary>
        /// Get the GroupType by id.
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public static GroupType GetGroupTypeByID(int id)
        {
            if (id <= 0)
                return null;
            else
            {
                using (cpeinEntities db = new cpeinEntities())
                {
                    var grt = from GroupType in db.GroupType
                              where GroupType.grt_id == id
                              select GroupType;
                    foreach(GroupType tmp in grt)
                        return tmp;
                    return null;
                }
            }
        }

        /// <summary>
        /// grt a GroupType into the database.
        /// </summary>
        /// <param name="grt"></param>
        /// <returns></returns>
        public static GroupType GroupTyperea(GroupType grt)
        {
            if (grt != null)
            {
                using (cpeinEntities db = new cpeinEntities())
                {
                    GroupType ngrt = db.GroupType.Add(grt);
                    db.SaveChanges();
                    return ngrt;
                }
            }
            else
                return null;
        }

        /// <summary>
        /// Remove a specific GroupType of the database.
        /// </summary>
        /// <param name="grt"></param>
        /// <returns></returns>
        public static bool RemoveGroupType(GroupType grt)
        {
            if (grt != null)
            {
                grt.grt_isdeleted = true;
                if (UpdateGroupType(grt) != null)
                {
                    using (cpeinEntities db = new cpeinEntities())
                    {
                        db.SaveChanges();
                        return true;
                    }
                }
            }

            return false;
        }

        /// <summary>
        /// Update the GroupType into the database.
        /// </summary>
        /// <param name="grt"></param>
        /// <returns></returns>
        public static GroupType UpdateGroupType(GroupType grt)
        {
            if (grt != null)
            {
                using (cpeinEntities db = new cpeinEntities())
                {
                    var current = from GroupType in db.GroupType
                                  where GroupType.grt_id == grt.grt_id
                                  select GroupType;

                    foreach (var tmp in current)
                    {
                        tmp.grt_label = grt.grt_label;
                        tmp.grt_isdeleted = grt.grt_isdeleted;

                        db.SaveChanges();
                    }

                    grt = current as GroupType;
                }
            }

            return grt;
        }
    }
}
